package org.sl.flvplayer.view {
	import org.sl.flvplayer.controls.Controller;
	import org.sl.flvplayer.data.VideoItem;

	import flash.display.Sprite;

	/**
	 * Initializes the PlayerDisplay 
	 * 
	 * @package org.sl.flvplayer.view
	 * @author Dan
	 * @version
	 * @see org.sl.flvplayer.view.PlayerLogic
	 * @see org.sl.flvplayer.view.PlayerDisplay
	 * @see org.sl.flvplayer.view.PlayerScreenInteraction
	 * @see org.sl.flvplayer.controls.Controller
	 */ 
	public class PlayerScreen extends Sprite {
		
		private static var instance : PlayerScreen; 
		private var background : Sprite = new Sprite(); 
		private var _source : VideoItem;
	
		/**
		 * playerDisplay
		 */
		public var playerDisplay : PlayerDisplay;
		
		/**
		 * playerScreenInteraction
		 */
		public var playerScreenInteraction : PlayerScreenInteraction;		

		/**
		 * Ensures that there is only one instance of the class by using the singleton pattern.
		 * 
		 * @return instance of the <code>PlayerScreen</code> class
		 */
		public static function getInstance() : PlayerScreen {
			if(!instance) instance = new PlayerScreen();
			return instance;
		}

		/**
		 * 
		 */
		function PlayerScreen() {    					
			super();							
		}

		/**
		 *  
		 */
		public function get source() : VideoItem {		
			return _source;
		}
		
		/**
		 * @param p_source current videoItem
		 */
		public function set source(p_source : VideoItem) : void {
			//p_source.ns.play(p_source.videoSource);
			//p_source.ns.pause();
			_source = p_source;
		}

		/**
		 * Initializes all methods responsible for adding the different playerScreen viewing parts.
		 */
		public function render() : void {	
			addplayerScreenBackground();		
			addPlayerDisplay();
			addPlayerScreenInteraction();			
		}

		/**
		 * Resets the playerScreen and its subelements to their initial state.</br>
		 * Furthermore closes the actual netstream and displays the initial skins of the play and the screen_play buttons.
		 */
		public function reset() : void {
			if(_source) {
				// to prevent unnecessary bandwith usage and overlapping of soundchannels
				_source.ns.close();
				_source.isPlaying = false;
				// initial state of buttons
				Controller.getInstance().changeStatus("play", true);
				Controller.getInstance().changeStatus("screen_play", true);
				// unload watermark and icon
				playerScreenInteraction.unloadIcon();
				playerScreenInteraction.unloadWatermark();
				playerDisplay.reset();
			}
		}

		/**
		 * Initializes all resize methods and checks whether an icon or a watermark are available and triggers their positioning method.
		 */
		public function resize() : void {
			resizeBackground();
			playerDisplay.resizePreviewimage();
			playerScreenInteraction.resizeScreenInteraction();
			// positioning of icon or watermark after the resize of the playerapplication
			if(source.iconAvailable) playerScreenInteraction.posIcon(playerScreenInteraction.icon);
			if(source.watermarkAvailable) playerScreenInteraction.posIcon(playerScreenInteraction.watermark);		
		}

		/**
		 * Adds the background of the playerScreen.
		 */
		private function addplayerScreenBackground() : void {
			addChildAt(background, 0);
			background.graphics.beginFill(_source.background, 0);
			background.graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
			background.graphics.endFill();
		}

		/**
		 * Adds the playerDisplay.
		 */
		private function addPlayerDisplay() : void {
			// playerDisplay is added only once to stage
			if(!playerDisplay) {
				playerDisplay = new PlayerDisplay(_source);
				addChildAt(playerDisplay,1);
			}
			 playerDisplay.addVideo(_source);
		}

		/**
		 * Adds the playerScreenInteraction.
		 */
		private function addPlayerScreenInteraction() : void {
			// playerScreenInteraction is added only once to stage
			if(!playerScreenInteraction) {
				playerScreenInteraction = PlayerScreenInteraction.getInstance();
				addChildAt(playerScreenInteraction,2);
				playerScreenInteraction.initPlayScreenInteraction();
			}
			if(source.watermarkAvailable) playerScreenInteraction.loadWatermark();
			if(source.iconAvailable) playerScreenInteraction.loadIcon();
		}

		/**
		 * Resizes the background to fit the player.
		 */
		private function resizeBackground() : void {
			background.width = stage.stageWidth;
			background.height = stage.stageHeight;
		}		
	}
}
